package com.yitong.profile.controller;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yitong.base.controller.BaseController;
import com.yitong.base.domain.Constant;
import com.yitong.profile.domain.Company;
import com.yitong.profile.service.CompanyService;

/**
 * 合作伙伴用户管理
 * 
 * @author 邓杰
 * @date 2015/10/22
 */
@Controller
@RequestMapping("/web/admin/company")
public class CompanyController extends BaseController {

	@Inject
	private CompanyService companyService;

	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "/companyList")
	public String companyList() {
		String target = "error";
		if (checkTabUserLogin()) {
			Map<String, Object> param = getParameters();
			Map<String, Object> result = new HashMap<String, Object>();
			String toResult = param.get("toResult").toString();
			try {
				switch (toResult) {
				case "company_list":
					target = "company/company_list";
					int pageNum = Integer.parseInt(param.get("pageNum") == null ? "0" : param.get("pageNum").toString());
					int pageSize = param.get("pageSize") == null ? Constant.DEFAULT_PAGE_SIZE : Integer.parseInt(param.get("pageSize").toString());
					page = companyService.listCompanyWithPage(param, pageNum, pageSize);
					result.put("page", page);
					result.put("code", 1);
					break;

				case "company_add":
					target = "company/company_add";
					break;
				case "company_update":
					target = "company/company_update";
					Company company = companyService.showCompanyById(param);
					request.setAttribute("company", company);
					break;
				case "company_delete":
					forDwzResult(companyService.delete(param));
					break;
				}
			} catch (Exception e) {
				result.put("code", 0);
				e.printStackTrace();
				logger.info("用户页面跳转出错，异常原因为：" + e);
			}
			super.setParameters(param);
			super.setParameters(result);
			return target;
		} else {
			target = "loginTab";
			return target;
		}

	}

	/**
	 * 添加
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addCompany", method = RequestMethod.POST)
	public void addCompany() {
		if (checkTabUserLogin()) {
			Map<String, Object> param = getParameters();
			try {
				forDwzResult(companyService.insert(param));
				logger.info("添加用户成功");
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("添加用户出错，异常原因为：" + e);
			}
		}
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateCompany", method = RequestMethod.POST)
	public void updateCompany() {
		if (checkTabUserLogin()) {
			Map<String, Object> param = getParameters();
			try {
				forDwzResult(companyService.update(param));
				logger.info("编辑用户成功");
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("编辑用户出错，异常原因为：" + e);
			}
		}
	}

}
